package cn.csc.module.member.controller.app.userrole;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.member.controller.app.userrole.vo.*;
import cn.csc.module.member.convert.userrole.MemberUserRoleConvert;
import cn.csc.module.member.dal.dataobject.userrole.MemberUserRoleDO;
import cn.csc.module.member.service.userrole.MemberUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "用户 APP - 会员和角色关联")
@RestController
@RequestMapping("/member/user-role")
@Validated
public class AppMemberUserRoleController {

    @Resource
    private MemberUserRoleService userRoleService;

    @PostMapping("/create")
    @ApiOperation("创建会员和角色关联")

    public CommonResult<BigInteger> createUserRole(@Valid @RequestBody AppMemberUserRoleCreateReqVO createReqVO) {
        return success(userRoleService.createUserRole(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新会员和角色关联")

    public CommonResult<Boolean> updateUserRole(@Valid @RequestBody AppMemberUserRoleUpdateReqVO updateReqVO) {
        userRoleService.updateUserRole(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除会员和角色关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)

    public CommonResult<Boolean> deleteUserRole(@RequestParam("id") BigInteger id) {
        userRoleService.deleteUserRole(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得会员和角色关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)

    public CommonResult<AppMemberUserRoleRespVO> getUserRole(@RequestParam("id") BigInteger id) {
        MemberUserRoleDO userRole = userRoleService.getUserRole(id);
        return success(MemberUserRoleConvert.INSTANCE.convert(userRole));
    }

    @GetMapping("/list")
    @ApiOperation("获得会员和角色关联列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)

    public CommonResult<List<AppMemberUserRoleRespVO>> getUserRoleList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<MemberUserRoleDO> list = userRoleService.getUserRoleList(ids);
        return success(MemberUserRoleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得会员和角色关联分页")

    public CommonResult<PageResult<AppMemberUserRoleRespVO>> getUserRolePage(@Valid AppMemberUserRolePageReqVO pageVO) {
        PageResult<MemberUserRoleDO> pageResult = userRoleService.getUserRolePage(pageVO);
        return success(MemberUserRoleConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出会员和角色关联 Excel")

    @OperateLog(type = EXPORT)
    public void exportUserRoleExcel(@Valid AppMemberUserRoleExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberUserRoleDO> list = userRoleService.getUserRoleList(exportReqVO);
        // 导出 Excel
        List<AppMemberUserRoleExcelVO> datas = MemberUserRoleConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员和角色关联.xls", "数据", AppMemberUserRoleExcelVO.class, datas);
    }

}
